package com.nl.controller.web.controller;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsTemplateAttr;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.global.GlobalConst;
import com.nl.controller.web.service.LoginService;
import com.nl.controller.web.service.SmsBusiService;
import com.nl.controller.web.service.SmsTemplateAttrService;
import com.nl.controller.web.util.SendSms;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("template")
@Slf4j
public class SmsTemplateAttrController {
    @Autowired
    private SmsTemplateAttrService smsTemplateAttrService;
    @Autowired
    private LoginService loginService;

    @Autowired
    private SmsBusiService smsBusiService;

    @Autowired
    private CommonService commonService;

    private SmsTemplateAttr templateAttr;

    @ApiOperation(value = "查询模板信息", notes = "分页查询模板信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "城市编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "mgrId", value = "项目编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "state", value = "模板状态", required = true, dataType = "String"),
            @ApiImplicitParam(name = "flag", value = "条件", required = true, dataType = "String")
    })
    @PostMapping("find")
    public Response findTemlpale(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam(value = "cityId", required = false) String cityId,
            @RequestParam(value = "mgrId", required = false) String mgrId,
            @RequestParam(value = "mgrName", required = false) String mgrName,
            @RequestParam(value = "state", required = false) String state,
            @RequestParam(value = "flag", required = false) String flag,
            @RequestParam(value = "duty", required = false) String duty,
            @RequestParam Map<String, Object> paramMap
    ) {
        if (!"0".equals(duty)&&("".equals(cityId) || cityId == null)) {
            throw new SmsException("请选择查询条件，开始查询");
        } else {
            PageInfo<SmsTemplateAttr> pageInfo = smsTemplateAttrService.findTempalteAttr(page, limit, cityId, mgrId, mgrName, state, flag);
            log.info(" 查询模板信息：{}", paramMap.toString());
            log.debug("查询模板信息返回参数：{}", pageInfo.getList().toString());
            return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
        }
    }


//    @ApiOperation(value = "配置模板信息", notes = "配置模板信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "mgrId", value = "项目id", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "thresholdStartTime", value = "周期范围开始时间", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "thresholdEndTime", value = "周期范围结束时间", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "everyDayStartTime", value = "每天开始时间", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "everyDayEndTime", value = "每天结束时间", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "realTime", value = "延时", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "isLocal", value = "本地发送", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "cycle", value = "周期", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "cycleThreshold", value = "周期数量", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "isThreshold", value = "是否设置阀值", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "threshold", value = "总阀值数量", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "localDate", value = "本地发送保存时间", required = false, dataType = "String"),
//            @ApiImplicitParam(name = "isProvince", value = "外地发送范围", required = false, dataType = "String"),
//            @ApiImplicitParam(name = "reset", value = "重置阀值", required = false, dataType = "String")
//    })
//    @PostMapping("configure")
//    public Response insertTemplate(
//            @RequestParam(value = "mgrId", required = false) String mgrId,
//            @RequestParam(value = "startTime", required = false) String startTime,
//            @RequestParam(value = "endTime", required = false) String endTime,
//            @RequestParam(value = "everyDayStartTime", required = false) String everyDayStartTime,
//            @RequestParam(value = "everyDayEndTime", required = false) String everyDayEndTime,
//            @RequestParam(value = "realTime", required = false) String realTime,
//            @RequestParam(value = "isLocal", required = false) String isLocal,
//            @RequestParam(value = "cycle", required = false) String cycle,
//            @RequestParam(value = "cycleThreshold", required = false) int cycleThreshold,
//            @RequestParam(value = "thresholdStartTime", required = false) String thresholdStartTime,
//            @RequestParam(value = "thresholdEndTime", required = false) String thresholdEndTime,
//            @RequestParam(value = "isThreshold", required = false) String isThreshold,
//            @RequestParam(value = "threshold", required = false) int threshold,
//            @RequestParam(value = "reset", required = false) String reset,
//            @RequestParam(value = "localDate", required = false) String localDate,
//            @RequestParam(value = "nonlocalDate", required = false) String nonlocalDate,
//            @RequestParam(value = "isProvince", required = false) String isProvince,
//            @RequestParam(value = "flag", required = false) String flag,
//            HttpSession session,
//            @RequestParam Map<String, Object> paramMap
//    ) {
//        log.info(" 配置模板参数：{}", paramMap.toString());
//        SmsUsers user = (SmsUsers) session.getAttribute("user");
//        String state = "0";
//        if (user.getDuty().equals("0")) {
//            state = "2";
//        }
//        int result = 0;
//        //参数处理
//        //是否设置阀值, 0 不设置  1设置发送总量 2 发送周期 3 全设置
//        if (("0").equals(isThreshold)) {
//            cycle = "0";
//            threshold = 0;
//            cycleThreshold = 0;
//        } else if ("1".equals(isThreshold)) {
//            cycle = "0";
//            cycleThreshold = 0;
//        } else if ("2".equals(isThreshold)) {
//            threshold = 0;
//        }
//        if ("".equals(everyDayStartTime) || everyDayStartTime == null) {
//            everyDayStartTime = "8:00";
//        }
//        if ("".equals(everyDayEndTime) || everyDayEndTime == null) {
//            everyDayEndTime = "20:00";
//        }
//        result = smsTemplateAttrService.configureTemplate(mgrId, startTime, endTime, thresholdStartTime, thresholdEndTime, everyDayStartTime, everyDayEndTime, realTime, isLocal, cycle, cycleThreshold, isThreshold, threshold, flag, state,localDate, nonlocalDate,isProvince);
//        log.debug("配置模板返回参数：{}", result);
//        if (result == 1) {
//            StringBuffer sbf = new StringBuffer();
//            String content = "项目:" + smsBusiService.findByMgrId(mgrId).getName() + "已完成配置，请省公司管理员审核";
//            sbf.append(content);
//            if ("modify".equals(flag)) {
//                //改动项
//                sbf.append("改动项:");
//                if (!(startTime).equals(templateAttr.getStartDate())) {
//                    sbf.append("  发送开始时间变为:" + startTime + ",原先:" + templateAttr.getStartDate());
//                }
//                String endDate=templateAttr.getEndDate()==null ?"":templateAttr.getEndDate();
//                if (!(endTime).equals(endDate)) {
//                    sbf.append("  发送结束时间变为:" + endTime + ",原先:" + templateAttr.getEndDate());
//                }
//                if (!(everyDayStartTime).equals(templateAttr.getEveryDayStartTime())) {
//                    sbf.append("  每日发送开始时间变为:" + everyDayStartTime + ",原先:" + templateAttr.getEveryDayStartTime());
//                }
//                if (!(everyDayEndTime).equals(templateAttr.getEveryDayEndTime())) {
//                    sbf.append("  每日发送结束时间变为:" + everyDayEndTime + ",原先:" + templateAttr.getEveryDayEndTime());
//                }
//                if (!(realTime).equals(templateAttr.getRealTime())) {
//                    sbf.append("  延时改为:" + realTime + "分钟" + ",原先:" + templateAttr.getRealTime());
//                }
//                if (!(isLocal).equals(templateAttr.getIsLocal())) {
//                    String local="";
//                    String prelocal="";
//                    if("1".equals(templateAttr.getIsLocal())){
//                        prelocal="外地";
//                    }else if("0".equals(templateAttr.getIsLocal())){
//                        prelocal="本地";
//                    }else if("2".equals(templateAttr.getIsLocal())){
//                        prelocal="本地+外地";
//                    }
//                    if("1".equals(isLocal)){
//                        local="外地";
//                        sbf.append(" 发送范围改为" + local+",原先："+prelocal);
//                        if (!(isProvince).equals(templateAttr.getIsProvince())) {
//                            sbf.append(" 本地号码保留时间改为" + isProvince+"天,原先："+templateAttr.getIsProvince()+"天,");
//                        }
//                    }else if("0".equals(isLocal)){
//                        local="本地";
//                        sbf.append(" 发送范围改为" + local+",原先："+prelocal+",");
//                        if (!(localDate).equals(templateAttr.getLocalDate())) {
//                            sbf.append(" 本地号码保留时间改为" + localDate+"天,原先："+templateAttr.getLocalDate()+"天,");
//                        }
//                        "0".equals(isProvince);
//                    }else if("2".equals(isLocal)){
//                        local="本地+外地";
//                        sbf.append(" 发送范围改为" + local+",原先："+prelocal+",");
//                        if (!(localDate).equals(templateAttr.getLocalDate())) {
//                            sbf.append(" 本地号码保留时间改为" + localDate+"天,原先："+templateAttr.getLocalDate()+"天,");
//                        }
//                        if (!(isProvince).equals(templateAttr.getIsProvince())) {
//                            sbf.append(" 本地号码保留时间改为" + isProvince+"天,原先："+templateAttr.getIsProvince()+"天,");
//                        }
//                    }
//
//                }
//                
//                if( !(isThreshold).equals(templateAttr.getIsThreshold())||threshold!=templateAttr.getThreshold()
//                       ||!(cycle).equals(templateAttr.getCycle()) ||cycleThreshold != templateAttr.getCycleThreshold()){
//                    String chooseCycle="";
//                    String preCycle="";
//                    if("1".equals(cycle)){
//                        chooseCycle="按年";
//                    }else if("2".equals(cycle)){
//                        chooseCycle="按月";
//                    }else if("3".equals(cycle)){
//                        chooseCycle="按周";
//                    }else if("4".equals(cycle)){
//                        chooseCycle="按日";
//                    }
//                    if("1".equals(templateAttr.getCycle())){
//                        preCycle="按年";
//                    }else if("2".equals(templateAttr.getCycle())){
//                        preCycle="按月";
//                    }else if("3".equals(templateAttr.getCycle())){
//                        preCycle="按周";
//                    }else if("4".equals(templateAttr.getCycle())){
//                        preCycle="按日";
//                    }
//                    sbf.append(" 之前总阀值:" + templateAttr.getThreshold() + "条,"+preCycle+"周期阀值:"+templateAttr.getCycleThreshold()+"条,");
//                    sbf.append(" 现在总阀值:" + threshold + "条,"+chooseCycle+"周期阀值:"+cycleThreshold+"条,");
//                }
//
//                if ("1".equals(reset)) {
//                    sbf.append("重置了阀值");
//                }
//               
//            }
//            //System.out.println("sbf.toString()========"+sbf.toString());
//            if (!"0".equals(user.getDuty())) {
//                String[] msisdn = commonService.templateNotice("1000000001");
//                if (msisdn != null && msisdn.length > 0) {
//                    for (String number : msisdn) {
//                      loginService.sendCode(SendSms.sendCode(number, sbf.toString()));
//                        log.info("手机号" + number);
//                        log.info(sbf.toString());
//                    }
//                }
//            }
//            //是否设置阀值, 0 不设置  1设置发送总量 2 发送周期 3 全设
//            HashMap map = new HashMap<String, Object>();
//            map.put("mgrId", mgrId);
//            map.put("reset", reset);
//            smsTemplateAttrService.resetThreshold(map);
//            return ResponseUtil.sucess("配置成功");
//        } else {
//            log.debug("配置模板返回参数：{}", result);
//            return ResponseUtil.error(result, "配置失败请重试");
//        }
//    }


    @ApiOperation(value = "查看模板信息", notes = "查看模板信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mgrId", value = "项目id", required = true, dataType = "String")
    })
    @PostMapping("findTemplate")
    public Response findTemplate(
            @RequestParam(value = "mgrId", required = false) String mgrId
    ) {
        SmsTemplateAttr smsTemplateAttr;
        smsTemplateAttr = smsTemplateAttrService.findTemplateAttrByMgrId(mgrId);
        templateAttr = smsTemplateAttr;
        log.info(" 项目信息：{}", smsTemplateAttr.toString());
        return ResponseUtil.sucess(smsTemplateAttr);
    }


}

